package com.ruoyi.web.controller.work;

import java.util.List;

import com.ruoyi.work.domain.WorkCustomer;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.work.domain.WorkClient;
import com.ruoyi.work.service.IWorkClientService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户管理Controller
 * 
 * @author ruoyi
 * @date 2021-12-21
 */
@Controller
@RequestMapping({"/work/client","/work/networkclient","/work/planeclient","/work/flowclient","/work/appclient","/work/wechatclient","/work/weiboclient"})
public class WorkClientController extends BaseController
{
    private String prefix = "work/client";

    @Autowired
    private IWorkClientService workClientService;

    @RequiresPermissions(value = {"work:client:view","work:appclient:view","work:networkclient:view","work:planeclient:view","work:flowclient:view","work:wechatclient:view","work:weiboclient:view"},logical = Logical.OR)
    @GetMapping()
    public String client()
    {
        return prefix + "/client";
    }


    /**
     * 查询客户管理列表
     */
    @RequiresPermissions(value = {"work:client:list","work:appclient:list","work:networkclient:list","work:planeclient:list","work:flowclient:list","work:wechatclient:list","work:weiboclient:list"},logical = Logical.OR)
    @PostMapping("/list/{clientType}")
    @ResponseBody
    public TableDataInfo list(@PathVariable("clientType") String clientType,WorkClient workClient)
    {
        startPage();
        workClient.setClientType(clientType);
        List<WorkClient> list = workClientService.selectWorkClientList(workClient);
        return getDataTable(list);
    }
    /**
     * 查询客户列表
     */
    @RequiresPermissions("work:client:query")
    @GetMapping ("/query/{clientType}")
    @ResponseBody
    public AjaxResult query(@PathVariable("clientType") String clientType,WorkClient workClient)
    {
        AjaxResult ajax = new AjaxResult();

        ajax.put("code", 200);
        workClient.setClientType(clientType);
        List<WorkClient> list = workClientService.selectWorkClientList(workClient);
        ajax.put("value", list);
        return ajax;
    }
    /**
     * 导出客户管理列表
     */
    @RequiresPermissions(value = {"work:client:export","work:appclient:export","work:networkclient:export","work:planeclient:export","work:flowclient:export","work:wechatclient:export","work:weiboclient:export"},logical = Logical.OR)
    @Log(title = "客户管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export/{clientType}")
    @ResponseBody
    public AjaxResult export(WorkClient workClient,@PathVariable("clientType") String clientType)
    {
        workClient.setClientType(clientType);
        List<WorkClient> list = workClientService.selectWorkClientList(workClient);
        ExcelUtil<WorkClient> util = new ExcelUtil<WorkClient>(WorkClient.class);
        return util.exportExcel(list, "客户管理数据");
    }

    /**
     * 新增客户管理
     */
    @GetMapping("/add/{clientType}")
    public String add(@PathVariable("clientType") String clientType, ModelMap mmap)
    {
        WorkClient workClient = new WorkClient();
        workClient.setClientType(clientType);
        mmap.put("workClient", workClient);
        return prefix + "/add";
    }

    /**
     * 新增保存客户管理
     */
    @RequiresPermissions(value = {"work:client:add","work:appclient:add","work:networkclient:add","work:planeclient:add","work:flowclient:add","work:wechatclient:add","work:weiboclient:add"},logical = Logical.OR)
    @Log(title = "客户管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(WorkClient workClient)
    {
        workClient.setCreateBy(getLoginName());
        return toAjax(workClientService.insertWorkClient(workClient));
    }

    /**
     * 修改客户管理
     */
    @RequiresPermissions(value = {"work:client:edit","work:appclient:edit","work:networkclient:edit","work:planeclient:edit","work:flowclient:edit","work:wechatclient:edit","work:weiboclient:edit"},logical = Logical.OR)
    @GetMapping("/edit/{clientId}")
    public String edit(@PathVariable("clientId") Long clientId, ModelMap mmap)
    {
        WorkClient workClient = workClientService.selectWorkClientByClientId(clientId);
        mmap.put("workClient", workClient);
        return prefix + "/edit";
    }

    /**
     * 修改保存客户管理
     */
    @RequiresPermissions(value = {"work:client:edit","work:appclient:edit","work:networkclient:edit","work:planeclient:edit","work:flowclient:edit","work:wechatclient:edit","work:weiboclient:edit"},logical = Logical.OR)
    @Log(title = "客户管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(WorkClient workClient)
    {
        return toAjax(workClientService.updateWorkClient(workClient));
    }

    /**
     * 删除客户管理
     */
    @RequiresPermissions(value = {"work:client:remove","work:appclient:remove","work:networkclient:remove","work:planeclient:remove","work:flowclient:remove","work:wechatclient:remove","work:weiboclient:remove"},logical = Logical.OR)
    @Log(title = "客户管理", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(workClientService.deleteWorkClientByClientIds(ids));
    }
}
